<template>
  <div
    class="article-list"
    ref="article_list"
  >
    <van-pull-refresh
      v-model="isLoading"
      :success-text="refreshSuccessText"
      :success-duration="1500"
      @refresh="onRefresh"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        @load="onLoad"
      >
        <van-cell
          v-for="(item,index) in arts"
          :key="index"
        >
          <ArticleItem :artItem="item"></ArticleItem>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArtList } from '@/api/article'
import ArticleItem from '@/components/article-item'
import { mapState } from 'vuex'
export default {
  name: 'ArticleList',
  components: {
    ArticleItem
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {
    user () {
      this.scroll = 0
      this.arts = []
      this.onLoad()
    }
  },
  props: {
    channel: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      arts: [],
      loading: false,
      finished: false,
      timestamp: null,
      error: false,
      isLoading: false,
      refreshSuccessText: '',
      scroll: null
    }
  },
  methods: {
    async onLoad () {
      try {
        const { data } = await getArtList({
          channel_id: this.channel.id,
          timestamp: this.timestamp || Date.now(), // 时间戳
          with_top: 1 // 是否包含置顶
        })
        const { results } = data.data
        this.arts.push(...results)
        this.loading = false

        // if (Math.random() > 0.5) {
        //   JSON.parse('asdasd')
        // }
        if (results.length) {
          this.timestamp = data.data.pre_timestamp
        } else {
          this.finished = true
        }
      } catch (err) {
        this.loading = false
        this.error = true
      }
    },
    async onRefresh () {
      try {
        const { data } = await getArtList({
          channel_id: this.channel.id,
          timestamp: Date.now(), // 时间戳
          with_top: 1 // 是否包含置顶
        })
        const { results } = data.data
        this.arts.unshift(...results)
        this.isLoading = false

        // if (Math.random() > 0.5) {
        //   JSON.parse('asdasd')
        // }
        this.refreshSuccessText = `刷新成功，更新了${results.length}条数据`
      } catch (err) {
        this.isRefreshLoading = false // 关闭下拉刷新的 loading 状态
        this.$toast('刷新失败')
      }
    },
    handleScroll () {
      this.scroll = this.$refs.article_list && this.$refs.article_list.scrollTop
      // console.log(this.scroll)
    }
  },
  mounted () {
    this.$refs.article_list.addEventListener('scroll', this.handleScroll)
  },
  activated () {
    if (this.scroll > 0) {
      this.$refs.article_list.scroll(0, this.scroll)
      this.scroll = 0
      this.$refs.article_list.addEventListener('scroll', this.handleScroll)
    }
  },
  deactivated () {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style scoped lang="less">
.article-list {
  height: calc(100vh - 280px);
  overflow: auto;
}
</style>
